//
//  YKDestinationViewController.m
//  yuke
//
//  Created by Dejun Liu on 15/1/5.
//  Copyright (c) 2015年 juwan. All rights reserved.
//

#import "YKDestinationViewController.h"
#import "YKDestinationCollectionViewCell.h"
#import "YKDSTHeaderCollectionReusableView.h"
#import "YKPlace.h"
#import "YKCountryPlace.h"

@interface YKDestinationViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSArray *cData;

@end

@implementation YKDestinationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"目的地";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(150, 150);
    layout.minimumInteritemSpacing = 8;
    layout.minimumLineSpacing = 8;
    layout.sectionInset = UIEdgeInsetsMake(0, 6, 6, 6);
    layout.headerReferenceSize = CGSizeMake(320, 50);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49-64) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = UIColorFromRGB(250, 245, 233);
    [self.collectionView registerNib:[UINib nibWithNibName:@"YKDestinationCollectionViewCell" bundle:nil]
          forCellWithReuseIdentifier:@"Cell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"YKDSTHeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCell"];
    [self.view addSubview:self.collectionView];
    [self.collectionView addHeaderWithTarget:self action:@selector(requestData)];
    [self.collectionView headerBeginRefreshing];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    YKCountryPlace *cPlace = [_cData objectAtIndex:section];
    return cPlace.items.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _cData.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        DLog(@"header");
        YKDSTHeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCell" forIndexPath:indexPath];
        reusableview = headerView;
        YKCountryPlace *cPlace = [_cData objectAtIndex:indexPath.section];
        headerView.titleLabel.text = cPlace.country;
    }
    
    if (kind == UICollectionElementKindSectionFooter) {
        DLog(@"footer");
    }
    
    return reusableview;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YKCountryPlace *cPlace = [self.cData objectAtIndex:indexPath.section];
    YKPlace *place = [cPlace.items objectAtIndex:indexPath.row];
    YKDestinationCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:place.pic]];
    cell.countryLabel.text = place.name;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YKDetailSecondViewController *vc = [[YKDetailSecondViewController alloc] init];
    vc.curPlace = [((YKCountryPlace *)[self.cData objectAtIndex:indexPath.section]).items objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 数据请求 -

- (void)requestData
{
    __weak YKDestinationViewController *weakSelf = self;
    [[JWWebService sharedInstance]getWithParams:@{@"module":@"destination"} jsonResponse:^(id responseData, NSError *error) {
        [weakSelf.collectionView headerEndRefreshing];
        if (responseData) {
            _cData = [YKCountryPlace listFromArrayDic:[responseData objectForKey:@"list"]];
            [_collectionView reloadData];
        }
    }];
}

@end
